package com.my.test.thread.aqs;

import java.util.concurrent.locks.Lock;

public class TwoLocksTest {

    final static Lock lock = new TwoLocks();

    public static class work extends Thread {
        @Override
        public void run() {
            while (true) {
                lock.lock();
                try {
                    Thread.sleep(1000);
                    System.out.println(Thread.currentThread().getName());
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock();
                }
            }
        }
    }

    // 启动10个线程
    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            // 在主线程关闭后无需手动关闭守护线程
            work w = new work();
            w.setDaemon(true);
            w.start();
        }

        // 每1秒换行
        for (int i = 0; i <10 ; i++) {
            try {
                Thread.sleep(1000);
                System.out.println();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }

}
